package controllers.manage.complaint;

import bill.Bill;
import controllers.BaseController;
import enums.BillOutStatus;
import enums.ReportUserType;
import enums.TrialType;
import order.OrderItem;
import order.Report;
import order.ReportRemark;
import play.Logger;
import play.mvc.With;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/8.
 */
@With(BaseController.class)
public class ComplaintCtrl extends BaseController {

    public static void  index(Integer pageNumber, String orderNumber, String startTime, String endTime){
        pageNumber = pageNumber == null ? 1 : pageNumber;
     /*   Map<String , Object> searchMap = new HashMap<>();
        if(StringUtil.isNotBlank(orderNumber)) {
            searchMap.put("orderNumber", orderNumber);
        }
        if(StringUtil.isNotBlank(startTime)) {
            searchMap.put("startTime", startTime+" 00:00:00");
        }
        if(StringUtil.isNotBlank(endTime)) {
            searchMap.put("endTime", endTime+" 00:00:00");
        }
        JPAExtPaginator<Report> reportList = Report.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        Logger.info("getPageCount : %s" , reportList.size());*/
        List<Report> reportList = Report.findAll();
        render(reportList,pageNumber);
    }
    /**
     * 投诉查询详细信息
     */
    public static void detail(long id, Integer pageNumber){
        Report report = Report.findById(id);
        List<ReportRemark> reportRemarks = null;
        if(report != null){
            reportRemarks = ReportRemark.getBydReport(report);
        }

        render(report,reportRemarks, pageNumber);
    }
    /**
     * 投诉 审判
     */
    public static void trialConfirm(ReportUserType userType, long  reportId){
        Logger.info("reportId=================== : %s" , reportId);
        Map<String , Object> resultMap = new HashMap<>();
        if(reportId > 0){
            Report report =  Report.findById(reportId);
            if(report == null){
                resultMap.put("success",false);
                resultMap.put("msg","投诉单不存在");
                renderJSON(resultMap);
            }
            OrderItem orderItem  = OrderItem.findById(report.orderItem.id);
            if(orderItem == null){
                resultMap.put("success",false);
                resultMap.put("msg","订单明细不存在");
                renderJSON(resultMap);
            }
            if(userType == ReportUserType.MERCHANT){
                report.trial = TrialType.MERCHANT;
            }else{
                report.trial = TrialType.USER;
            }
            report.save();
            // 冻结 明细
            Bill bill = Bill.getBillByOrderItem(orderItem);
            bill.outStatus = BillOutStatus.FROZEN;
            bill.save();
        }else{
            resultMap.put("success",false);
            resultMap.put("msg","投诉单不存在");
            renderJSON(resultMap);
        }
        resultMap.put("success",true);
        resultMap.put("msg","审判结束");
        renderJSON(resultMap);

    }



}
